<div class="layui-card layadmin-header">
    <div class="layui-breadcrumb" lay-filter="breadcrumb">
        <a lay-href="">主页</a>
        <a>应用管理</a>
        <a>渠道列表</a>
        <a><cite>编辑详情</cite></a>
    </div>
</div>
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-tab layui-tab-brief">
            <ul class="layui-tab-title">
                <li class="layui-this">编辑渠道参数</li>
            </ul>
            <div class="layui-tab-content">
                <style>
                    .layui-form-label {
                        width:15% !important;
                        margin-left:1%;
                    }

                    .layui-input , .layui-textarea{
                        width: 50%;
                    }

                </style>

                <form class="layui-form alipayChannel  layui-form-pane" style="display:none" id="aliFom" >
                    <div class="layui-form-item">
                        <label class="layui-form-label">渠道费率（%）</label>
                        <div class="layui-input-block">
                            <input type="text" name="rate" placeholder="请输入渠道费率" required  lay-verify="required"  autocomplete="off" class="layui-input alqdRate">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">合作伙伴身份（PID）</label>
                        <div class="layui-input-block">
                            <input type="text" name="partner" required  lay-verify="required" placeholder="请输入合作伙伴身份" autocomplete="off" class="layui-input copId">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">应用 App ID</label>
                        <div class="layui-input-block">
                            <input type="text" name="appid" required  lay-verify="required" placeholder="请输入应用 App ID" autocomplete="off" class="layui-input aliapid">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">支付宝账户</label>
                        <div class="layui-input-block">
                            <input type="text" name="ali_account" required  lay-verify="required" placeholder="请输入支付宝账户" autocomplete="off" class="layui-input aliAccount">
                        </div>
                    </div>
                    <div class="layui-form-item ">
                        <label class="layui-form-label">应用私钥</label>
                        <div class="layui-input-block">
                            <textarea  required name="private_key" lay-verify="required" placeholder="请输入应用私钥" class="layui-textarea apkey"></textarea>
                        </div>
                    </div>
                    <div class="layui-form-item ">
                        <label class="layui-form-label">支付宝公钥</label>
                        <div class="layui-input-block">
                            <textarea required name="alipay_public_key" lay-verify="required"  placeholder="请输入支付宝公钥" class="layui-textarea alikey"></textarea>
                        </div>
                    </div>
                    <div class="layui-form-item ">
                        <div class="layui-input-block">
                            <button type="button" lay-submit  lay-filter="btnAM" class="layui-btn">保存</button>
                            <a lay-href = "app/mch_channel/"  class="layui-btn ">返回</a>
                        </div>
                    </div>
                </form>


                <!-- 微信配置模块 -->
                <form class="layui-form wxChannel  layui-form-pane" style="display:none" id="wxFom">
                    <div class="layui-form-item">
                        <label class="layui-form-label">渠道费率（%）</label>
                        <div class="layui-input-block">
                            <input type="text" name="rate" placeholder="请输入渠道费率" required lay-verify="required" autocomplete="off" class="layui-input wxRate">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">应用 App ID</label>
                        <div class="layui-input-block">
                            <input type="text" name="appId" required  lay-verify="required" placeholder="请输入应用 App ID" autocomplete="off" class="layui-input wxAppId">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">应用 AppSecret</label>
                        <div class="layui-input-block">
                            <input type="text" name="appSecret" required  lay-verify="required" placeholder="请输入应用 AppSecret" autocomplete="off" class="layui-input wxAppSecret">
                        </div>
                    </div>
                    <div class="layui-form-item ">
                        <label class="layui-form-label">微信支付商户号</label>
                        <div class="layui-input-block">
                            <input type="text" name="mchId" required  lay-verify="required" placeholder="请输入微信支付商户号" autocomplete="off" class="layui-input wxMchId">
                        </div>
                    </div>
                    <div class="layui-form-item ">
                        <label class="layui-form-label">API密钥</label>
                        <div class="layui-input-block">
                            <textarea  required name="key" lay-verify="required"  placeholder="请输入API密钥" class="layui-textarea wxKey"></textarea>
                        </div>
                    </div>
                    <div class="layui-form-item ">
                        <label class="layui-form-label">API证书路径</label>
                        <div class="layui-input-block">
                            <textarea required name="certLocalPath" lay-verify="required" placeholder="请输入API证书路径,请联系客服上传证书" class="layui-textarea wxCertLocalPath"></textarea>
                        </div>
                    </div>
                    <div class="layui-form-item ">
                        <div class="layui-input-block ">
                            <button type="button" lay-submit  lay-filter="btnWM" class="layui-btn">保存</button>
                            <a lay-href = "app/mch_channel/"  class="layui-btn ">返回</a>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
<script>

    layui.use(['admin', 'form','table','util'],function(){
        var form = layui.form
            , $ = layui.$
            , admin = layui.admin
            , layer = layui.layer
            , element = layui.element
            , table = layui.table;


        element.render('breadcrumb', 'breadcrumb');//渲染导航信息

        var router = layui.router();

        var curAppId = router.search.id;

        var curChanId = router.search.channelId;

        var curChanType = router.search.channelType;




        admin.req({
            type: 'post',
            url: layui.setter.baseUrl + '/mch_channel/get',
            data: {
                id:curAppId,
                channelId:curChanId
            },
            error: function(err){
                layer.alert(err);
            },
            success: function(res){
                if(res.code == 0){
                    if(res.data.id){
                        // console.log('商户渠道id不为空:',res.data.id)
                        shqdid = res.data.id;
                        crumchid = res.data.channelMchId;

                        // 支付宝部分
                        $('.alqdRate').val(JSON.parse(res.data.param).rate);
                        $('.altateid').val(JSON.parse(res.data.param).appid);
                        $('.copId').val(JSON.parse(res.data.param).partner);
                        $('.aliAccount').val(JSON.parse(res.data.param).ali_account);
                        $('.apkey').val(JSON.parse(res.data.param).private_key);
                        $('.alikey').val(JSON.parse(res.data.param).alipay_public_key);
                        // 微信部分
                        $('.wxRate').val(JSON.parse(res.data.param).rate);
                        $('.wxAppId').val(JSON.parse(res.data.param).appId);
                        $('.wxAppSecret').val(JSON.parse(res.data.param).appSecret);
                        $('.wxKey').val(JSON.parse(res.data.param).key);
                        $('.wxMchId').val(JSON.parse(res.data.param).mchId);
                        $('.wxCertLocalPath').val(JSON.parse(res.data.param).certLocalPath);
                    }else {
                        // console.log('商户渠道id为空')
                        shqdid = '';
                    }
                }else{
                    layer.alert(res.msg,{title:"请求失败"})
                }
            }
        })


        switch(curChanType){
            case 'wxpay':
                $('.wxChannel').css('display','block');
                // 监听提交---微信
                form.on('submit(btnWM)', function(data){
                    if(!crumchid) {
                        layer.alert("请输入渠道商户ID",{title: '提示'});
                        return false;
                    }
                    // var cruRm = $.trim($('.rmark').val());
                    // var creS = $.trim($('input[name="state"]:checked').val());
                    admin.req({
                        type: 'post',
                        url: layui.setter.baseUrl + '/mch_channel/update', //模拟接口,
                        data: {
                            id:shqdid,
                            appId:curAppId,
                            channelId:curChanId,
                            param:JSON.stringify(data.field),
                            channelMchId:crumchid,
                            // state:creS,
                            // remark:cruRm
                        },
                        error: function(err){layer.alert(err.msg,{title:"请求失败"})},
                        success: function(res){
                            if(res.code == 0){
                                /*layer.alert(res.msg, {title: '保存成功'},function(index){
                                    layer.close(index);
                                    location.hash = '/app/mch_channel/';
                                });*/
                                layer.msg('保存成功', {
                                    offset: '15px'
                                    ,icon: 1
                                    ,time: 1000
                                }, function(){
                                    location.hash = '/app/mch_channel/';
                                });
                            }else {
                                /*layer.alert(res.msg,{title: '保存失败'})*/
                                layer.msg('保存失败', {
                                    offset: '15px'
                                    ,icon: 1
                                    ,time: 1000
                                });
                            }

                        }
                    })
                    return false;
                });
                break;
            case 'alipay':
                $('.alipayChannel').css('display','block');
                //监听提交---支付宝
                form.on('submit(btnAM)', function(data){
                    if(!crumchid) {
                        layer.alert("请输入渠道商户ID",{title: '提示'});
                        return false;
                    }
                    // var cruRm = $.trim($('.rmark').val());
                    // var creS = $.trim($('input[name="state"]:checked').val());
                    admin.req({
                        type: 'post',
                        url: layui.setter.baseUrl + '/mch_channel/update', //模拟接口,
                        data: {
                            id: shqdid,
                            appId: curAppId,
                            channelId: curChanId,
                            param:JSON.stringify(data.field),
                            channelMchId:crumchid,
                            // state:creS,
                            // remark:cruRm
                        },
                        error: function(err){layer.alert(err.msg,{title:"请求失败"})},
                        success: function(res){
                            if(res.code == 0){
                                /*layer.alert(res.msg, {title: '保存成功'},function(index){
                                    layer.close(index);
                                    location.hash = '/app/mch_channel/';
                                });*/
                                layer.msg('保存成功', {
                                    offset: '15px'
                                    ,icon: 1
                                    ,time: 1000
                                }, function(){
                                    location.hash = '/app/mch_channel/';
                                });
                            }else {
                                /*layer.alert(res.msg,{title: '保存失败'})*/
                                layer.msg('保存失败', {
                                    offset: '15px'
                                    ,icon: 1
                                    ,time: 1000
                                });
                            }
                        }
                    })
                    return false;
                });
                break;
        }
    });

</script>

